//index.js
//获取应用实例
const App = getApp()
let AJAX = require("../../utils/util.js").AJAX
let imgUrl = require("../../utils/util.js").imgUrl
let timeTranfer = require("../../utils/util.js").timeTranfer
Page({  
   data: { 
      imgUrl: imgUrl,
      navH: '', 
      typeList: [],    
      bannerList: [], 
      adList: [],
      noticeList: [],
      indicatorDots: true,
      autoplay: true,
      interval: 5000,
      duration: 1000, 
      currentNum: 0,
      showBtns: false, // 电话弹出控制
      contact: false,   
      playList: [], // 一千种玩法     
      isDisabled: false, //是否可以点击
      uname: '', // 留言用户
      uphone: '', // 用户电话
      umsg: '', // 用户留言,     
      videoList:[],
      news:'', 
    
      
   },
   onLoad: function() {
     
      let vm =this
      
      this.setData({
         navH: App.globalData.navHeight,
        
      })  
      
      if (!wx.getStorageSync("ACCESSTOKEN")){
          App.userInfoReadyCallback=res=>{ 
            //console.log(res)
            wx.setStorageSync('ACCESSTOKEN',res);
           // console.log(wx.getStorageInfoSync('ACCESSTOKEN'))
         
            vm.requestType()                         
            vm.requestCarousel()           
            vm.requestAd()        
            vm.requestNotice()          
            vm.requestPlay()// 一千种玩法           
             vm.requestVideo()
             vm.getNews()
          
         }
      
      }
    
 
      if (this.data.adList.length > 1) {
         this.setData({
            adver_autoplay: true
         })
      }
   },
   /**
    * 生命周期函数--监听页面显示
    */
   onShow: function() {
     let vm= this

     if (!wx.getStorageSync("ACCESSTOKEN")) {
       App.userInfoReadyCallback = res => {
         //console.log('Callback')
         wx.setStorageSync('ACCESSTOKEN', res);
         vm.requestType()
         vm.requestCarousel()
         vm.requestAd()
         vm.requestNotice()
         vm.requestPlay()// 一千种玩法           
         vm.requestVideo()
         vm.getNews()
       }

     } else {
       vm.requestType()
       vm.requestCarousel()
       vm.requestAd()
       vm.requestNotice()
       vm.requestPlay()// 一千种玩法           
       vm.requestVideo()
       vm.getNews()
     }

  

   },
   move() { },
   // 菜单接口
   requestType() {
      let vm = this
      AJAX("nav/nav-list",'GET',{
         len: 3
      }).then(res => {

         vm.setData({
            typeList: res.data
         })
      })
   },  
 

   requestCarousel() {
      let vm = this
      AJAX("carousel/info", "GET", {
         key: 'banner'
      }).then(res => { 
         
         vm.setData({
            bannerList: res.data.item,

         })
      })
   },
   // 站内信
   requestNotice() {
      let vm = this
      AJAX("message/get-message").then(res => {
         vm.setData({
            noticeList: res.data
         })
      })
   },
   // 评测 特惠 视频库
   requestAd() {
      let vm = this
      AJAX("carousel/info", "GET", {
         key: 'index_middle'
      }).then(res => {
         vm.setData({
            adList: res.data.item
         })

      })
   },
   // 一千零一种玩法	
   requestPlay() {
      let vm = this
      AJAX('idea/first').then(res => {

         //获取成功存储id 进行详情页的使用
         wx.setStorageSync('DETAILID', res.data.id)
         vm.setData({
            playList: res.data
         })
         // 获取id
         // console.log(wx.getStorageSync('DETAILID'))
      })
   },
   //videoList
   requestVideo(){

      let vm = this
      AJAX('video/index').then(res => {

         vm.setData({
            videoList: res.data
         })
      })
   },
   // 视频详情
   videoInfo(e) {
      let _id = e.currentTarget.dataset.id
      wx.navigateTo({
         url: '/pages/VideoLibrary/videoDetail/videoDetail?id=' + _id,
      })
   },
   // 更多视频
   videoMore() {
      wx: wx.navigateTo({
         url: '/pages/VideoLibrary/videoList/videolist',
         // success: function(res) {},
         // fail: function(res) {},
         // complete: function(res) {},
      })
   },
   getNews() {

      let vm = this
      AJAX('articles', "GET", {
         cid: 6
      }).then(res => {
         res.data.items.forEach(item => {
            item.published_at = timeTranfer(item.published_at, 1)
         })
         vm.setData({
            news: res.data.items
         })
      })
   },
   // 新闻详情
   newsInfo(e) {
      let _id = e.currentTarget.dataset.id
      wx: wx.navigateTo({
         url: '/pages/companycurse/companyinfo/companyinfo?id=' + _id,

      })
   },
   newsMore() {
      wx: wx.navigateTo({
         url: '/pages/companycurse/curselist/curselist',

      })

   },

   //导航跳转
 
   makePhone(e) {
      let _phone = e.currentTarget.dataset.phone
      if (!_phone) {
         return
      }
      wx.makePhoneCall({
         phoneNumber: _phone,
      })
   },
 
   // 跳转1001
   PM101(e){      
      wx.navigateTo({
         url: '/pages/pm101/pm101',
      })
      let id= e.currentTarget.dataset.id
      wx.setStorageSync('id', id)
      
   },


 

   /**留言 */
   confirmMsg() {
      let vm = this
      AJAX('applicant/submit-applicant', 'POST', {
         applicant_type: 'parents',
         applicant_name: this.data.uname,
         applicant_phone: this.data.uphone,
         applicant_message: this.data.umsg
      }).then(res => {
         if (res.status === 'succeed') {
            wx.showToast({
               title: '留言成功，我们将尽快与您联系',
               icon: "none"
            })
            this.setData({
               contact: false
            })

         }
      })

   },
   inputInfo(e) {
      this.setData({
         uname: e.detail.value
      })
      //console.log(e.detail.value)
   },
   inputInfoPhone(e) {
      this.setData({
         uphone: e.detail.value
      })
      //console.log(e.detail.value)
   },
   inputInfoMsg(e) {
      this.setData({
         umsg: e.detail.value
      })
      //console.log(e.detail.value)
   },
   navBack() {
      wx.navigateTo({
         url: '/pages/notice/notice',
      })
   },
   toCompany() {
      wx.navigateTo({
         url: '/pages/company/comintro/comintro',
      })
      // wx.navigateTo({
      //   url: '/pages/company/introduction/introduction',
      // })
   },


   routerToList(e) {
      let _id = e.currentTarget.dataset.id
      let _type = e.currentTarget.dataset.type
      if (_type == 2) {
         wx.navigateTo({
            url: '/pages/product/productlist/productlist?id=' + _id,
         })
         return
      }
      wx.navigateTo({
         url: '/pages/product/curselist/curselist?id=' + _id,
      })

   },
   toProductList(e) {
      let _type = e.currentTarget.dataset.type
      wx.navigateTo({
         url: '/pages/product/productlist/productlist?type=hot'
      })
   },
   toProduct(e) {
      let _id = e.currentTarget.dataset.id
      wx.navigateTo({
         url: '/pages/product/productinfo/productinfo?id=' + _id,
      })
   },
   tocurseList() {
      wx.navigateTo({
         url: '/pages/companycurse/curselist/curselist',
      })
   },
   toInfo(e) {
      let _id = e.currentTarget.dataset.id
      wx.navigateTo({
         url: '/pages/companycurse/companyinfo/companyinfo?id=' + _id,
      })
   },

   toSearchList(e) {
      let _val = e.detail.value
      wx.navigateTo({
         url: '/pages/product/search/search?val=' + _val,
      })
   },
   showBtns() {
      // console.log('1111')
      this.setData({
         contact: true
      })
   },
   closeContactDialog() {
      this.setData({
         contact: false
      })
   },
   closeCouponDialog() {
      this.setData({
         couponDialog: false,
         contact: false
      })
   },
   scEvent() {
      return false
   }
})